package com.example.myexamproject;



import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myexamproject.bean.Skin;
import com.example.myexamproject.utils.MySQLiteOpenHelper;


public class UpdateGamesActivity extends AppCompatActivity implements View.OnClickListener{
    //组件定义
    private EditText etskinid;
    private EditText etskinname;
    private EditText etwear;
    private EditText ettradenote;
    private EditText etprice;
    private Button btnSearch;
    private Button btnEdit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_games);
        initView();
    }

    private void initView() {
        etskinid = findViewById(R.id.et_skinid);
        btnSearch = findViewById(R.id.btn_search);
        etskinname = findViewById(R.id.et_skinname);
        etwear = findViewById(R.id.et_wear);
        ettradenote = findViewById(R.id.et_tradenote);
        etprice = findViewById(R.id.et_price);
        btnEdit=  findViewById(R.id.btn_edit);


        btnSearch.setOnClickListener((View.OnClickListener) this);
        btnEdit.setOnClickListener((View.OnClickListener) this);
    }
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_search:
                searchOrder();
                break;
            case R.id.btn_edit:
                updateOrder();
                break;
        }
    }
    private void searchOrder() {
        String skinid=etskinid.getText().toString().trim();
        MySQLiteOpenHelper dao=new MySQLiteOpenHelper(getApplicationContext());
        dao.open();
        Skin game=dao.getGames(skinid);
        etskinname.setText(game.skinname);
        etwear.setText(game.wear);
        ettradenote.setText(game.tradenote);
        etprice.setText(game.price);


        dao.close();
    }

    private void updateOrder() {
        Skin game=new Skin();
        game.skinid=etskinid.getText().toString().trim();
        game.skinname=etskinname.getText().toString().trim();
        game.wear=etwear.getText().toString().trim();
        game.tradenote=ettradenote.getText().toString().trim();
        game.price=etprice.getText().toString().trim();

        MySQLiteOpenHelper dao=new MySQLiteOpenHelper(getApplicationContext());
        dao.open();
        long result= dao.updateGames(game);
        if(result>0) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
        }
        dao.close();
    }
}
